﻿//-----------------------------------------------------------------------
// <copyright file="DataFetchWorker.cs" company="n.a.">
//     Copyright Dave999. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented", Justification = "To be returned to .")]

namespace DbView
{
	using System;
	using System.Collections.Generic;
	using System.Text;
	using System.Windows.Forms;
	using System.Threading;
	using System.Data;

	public class DataFetchWorker : WinFormThreading.WorkerBase
	{
		public DataFetchWorker(ContainerControl sender, Delegate senderDelegate, WinFormThreading.ThreadProcessState state) :
			base(sender, senderDelegate, state)
		{
		}
		public Model Model { get { return this.model; } set { this.model = value; } }
		public ViewContext Context { get { return this.viewContext; } set { this.viewContext = value; } }
		public DataTable Results { get { return this.datatable; } set { this.datatable = value; } }
		public Command Command { get { return this.command; } set { this.command = value; } }

		protected override void DoWorkStep(WinFormThreading.ThreadProcessState processState)
		{
			this.threadState = processState;
			try
			{
				this.datatable = this.model.DataSource.BuildTable(this.command, this.viewContext);
			}
			catch (DbViewApplicationException ex)
			{
				processState.setError(ex); // should abort
			}
			catch (Exception ex)
			{
				processState.setError(ex); // should abort
			}
		}
		public void Cancel()
		{
			if (this.threadState.IsWorking)
				this.threadState.cancel();
		}

		Model model;
		DataTable datatable;
		ViewContext viewContext;
		Command command;

		private WinFormThreading.ThreadProcessState threadState;
	}
}
